import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

# 加载数据
names = ['成色', '市场价格', '颜色', '材质', '是否限量', '回收价格']
# 读取数据，第0行是header
df = pd.read_csv('data.csv', names=names, sep='\t', header=0)
feature_names = ['成色', '市场价格', '颜色', '材质', '是否限量']
target_name = ['回收价格']

X = pd.DataFrame(df, columns=feature_names)
y = pd.DataFrame(df, columns=target_name)


# 数据归一化
# 先去掉异常值再归一化
# X 归一到 [0, 1]
# from sklearn import preprocessing
# min_max_scaler = preprocessing.MinMaxScaler()
# X = min_max_scaler.fit_transform(X)


# chi2校验
test = SelectKBest(score_func=chi2, k=2)
fit = test.fit(X, y.astype('int'))
np.set_printoptions(precision=3)
print('卡方校验得分',fit.scores_)

features = fit.transform(X)
# 特征中排名前5的值
print('特征值样本数据', features[0:5, :])